package com.androidtv.pos.ui.fragment.mealfragment;

import android.os.Bundle;
import android.view.View;

import com.androidhelper.module.common.base.BaseFragment;
import com.androidhelper.module.common.bean.BaseResponse;
import com.androidhelper.module.common.http.control.BaseObserver;
import com.androidhelper.module.common.http.control.RxSchedulers;
import com.androidtv.pos.R;
import com.androidtv.pos.adapter.RightMealAdapter;
import com.androidtv.pos.app.Constants;
import com.androidtv.pos.controller.CurrentMealController;
import com.androidtv.pos.http.repo.MineRepo;
import com.androidtv.pos.modelbean.HomeArticleData;
import com.androidtv.pos.modelbean.ProjectListData;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observable;

public class MealFragment extends BaseFragment implements CurrentMealController.onMealClickListener {

    // @BindView(R.id.smartRefreshLayout)
    //  SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.right_mainfrag_meal_recyclerview)
    RecyclerView mRecyclerView;


    // private static final String POSITION = "position";
    //  private static final String TITILE = "titile";
    private String mTitile;
    private int mPosition;
    private String mTag_tab_name;
    private int mBundle_project_id;
    private List<HomeArticleData> mHomeArticleDataList;

    private RightMealAdapter mRightMealAdapter;

    public static MealFragment newInstance(String tabName, int id) {

        Bundle args = new Bundle();
        args.putString(Constants.TAG_TAB_NAME, tabName);
        args.putInt(Constants.BUNDLE_PROJECT_ID, id);

        MealFragment fragment = new MealFragment();
        fragment.setArguments(args);
        return fragment;

    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CurrentMealController.getInstance().registerListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        CurrentMealController.getInstance().unregisterListener(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_meal;
    }


    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState) {
        /*Bundle args = getArguments();
        if (args != null) {
            mTag_tab_name = args.getString(Constants.TAG_TAB_NAME);
            mBundle_project_id = args.getInt(Constants.BUNDLE_PROJECT_ID);

        }*/
        // textView.setText(mTag_tab_name+" "+mBundle_project_id);

        //   smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        //   smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));

        initSecondTabRecyclerView();


    }

    private void initSecondTabRecyclerView() {

        mRecyclerView.setHasFixedSize(true);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(_mActivity, 4);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        mHomeArticleDataList = new ArrayList<>();
        mRightMealAdapter = new RightMealAdapter(R.layout.article_project_item_cardview_layout);
        mRecyclerView.setAdapter(mRightMealAdapter);
    }


    @Override
    protected void setListener() {
//        mRightMealAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                //页面跳转(略)
//
//                HomeArticleData homeArticleData = (HomeArticleData)adapter.getItem(position);
//                ToastUtils.showShort(homeArticleData.getTitle()+"  "+position);
//            }
//        });
//
//       // setSmartRefreshLayoutRefreshLoadMoreListener();
//
//        setMealAdapterItemChildClickListener();

        mRightMealAdapter.addListener(new RightMealAdapter.onButtonClickListener() {
            @Override
            public void onAdapterAddClick(HomeArticleData data) {
                CurrentMealController.getInstance().notifyAccountChange(data,null);

            }

            @Override
            public void onAdapterMinusClick(HomeArticleData data) {
                CurrentMealController.getInstance().notifyAccountChange(null,data);
            }

            @Override
            public void onAdaptrPreviewClick(HomeArticleData data) {
                //页面跳转(略)
//
//                HomeArticleData homeArticleData = (HomeArticleData)adapter.getItem(position);
//                ToastUtils.showShort(homeArticleData.getTitle()+"  "+position);
            }
        });

    }

    private int num = 0;





    /*private AddSubtractMealChangeCallBackListener mAddSubtractMealChangeCallBackListener;
    public void setAddSubtractMealChangeCallBackListener(AddSubtractMealChangeCallBackListener mAddSubtractMealChangeCallBackListener) {
        this.mAddSubtractMealChangeCallBackListener=mAddSubtractMealChangeCallBackListener;

    }*/





    /*private void setSmartRefreshLayoutRefreshLoadMoreListener() {
        //下拉刷新
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getProjectDate(mBundle_project_id,1,false);
                refreshLayout.autoRefresh();
            }
        });

        //加载更多
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getProjectDate(mBundle_project_id,curPage,true);
                refreshLayout.autoLoadMore();
            }
        });


    }*/

    @Override
    protected void initEventAndData(Bundle savedInstanceState) {
        Bundle args = getArguments();
        if (args != null) {
            mTag_tab_name = args.getString(Constants.TAG_TAB_NAME);
            mBundle_project_id = args.getInt(Constants.BUNDLE_PROJECT_ID);
            ToastUtils.showShort(mTag_tab_name + " " + mBundle_project_id);
        }

        //  if(-1 != mBundle_project_id){
        getProjectDate(mBundle_project_id, 1, false);
        //  }


    }


    //  private int curPage = 0;

    //获取项目模块数据
    private void getProjectDate(int projectId, int pageNum, boolean isRefreshData) {

        Observable<BaseResponse<ProjectListData>> projectListData = MineRepo.getInstance().getService().getProjectListData(pageNum, projectId);
        projectListData.compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<ProjectListData>() {
                    @Override
                    public void onSuccess(ProjectListData projectListData) {
                        //  curPage = projectListData.getCurPage()+1;
                        List<HomeArticleData> homeArticleDataList = projectListData.getDatas();

                        if (isRefreshData) {//加载更多 第二三页
                            mRightMealAdapter.addData(homeArticleDataList);
                        } else { //刷新数据  第一页数据刷新
                            mHomeArticleDataList.clear();
                            mHomeArticleDataList.addAll(homeArticleDataList);
                            mRightMealAdapter.replaceData(homeArticleDataList);
                        }

                        // smartRefreshLayout.finishRefresh();
                        // smartRefreshLayout.finishLoadMore();
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {

                    }
                });
    }


    @Override
    public void onAddClick(HomeArticleData data) {
        for (HomeArticleData datum : mRightMealAdapter.getData()) {
            if (datum.getId()==data.getId()){
                datum.setCurrentNum(data.getCurrentNum());
                mRightMealAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onMinusClick(HomeArticleData data) {
        for (HomeArticleData datum : mRightMealAdapter.getData()) {
            if (datum.getId()==data.getId()){
                datum.setCurrentNum(data.getCurrentNum());
                mRightMealAdapter.notifyDataSetChanged();
            }
        }
    }
}
























